﻿Ext.define('passport.controller.LoginController', {
    extend: 'Ext.app.Controller',

    stores: ['SysUserStore'],

    model: ['passport.model.SysUserModel'],

    views: ['passport.login.LoginForm'],

    init: function() {

        this.control({
            'loginForm button[action=login]': {
                click: this.loginSys
            },
	    'loginForm button[action=cancel]': {
	    	click: this.cancel
	    }
        });
    },

    loginSys: function(button) {
	var form = button.up('loginForm').getForm();
	if (form.isValid()) {
            form.submit({
		clientValidation: true, 
		method: 'post',
		success: function (form, action) {
			var success = action.result.success;
			var url = action.result.msg;
			if(success){
				console.log('login success');
				window.location.href = '/passport/entry.aspx';
			}
		},
		failure: function(form, action) {
			console.log('login failed');
			switch (action.failureType) {
            			case Ext.form.action.Action.CLIENT_INVALID:
                			Ext.Msg.alert('Failure', 'Form fields may not be submitted with invalid values');
                			break;
            			case Ext.form.action.Action.CONNECT_FAILURE:
                			Ext.Msg.alert('Failure', 'Communication failed');
                			break;
            			case Ext.form.action.Action.SERVER_INVALID:
               				Ext.Msg.alert('Failure', action.result.msg);
      					break;
			};
		}, 
		url: 'http://192.168.1.116/RpcService/Gateway.ashx?module=login&action=Login&method=login',
		waitMsg: 'Checking your account...'
	    });
	}
    },
    cancel: function(button) {
	var form = button.up('loginForm').getForm();
	form.reset();
    }
});
